package com.linyu.linpicturebackend.common.base;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.linyu.linpicturebackend.common.exception.ErrorCode;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * BaseResponse 全局响应封装类
 *
 * @author liyang
 * @since 2024/12/15 22:46
 */
public class BaseResponse<T> implements Serializable {

    private int code;

    private T data;

    private String message;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime timeStamp;

    public BaseResponse() {
    }

    public BaseResponse(int code, T data, String message) {
        this.code = code;
        this.data = data;
        this.message = message;
        this.timeStamp = LocalDateTime.now();
    }

    public BaseResponse(int code, T data){
        this(code, data, "");
    }

    public BaseResponse(ErrorCode errorCode){
        this(errorCode.getCode(), null, errorCode.getMessage());
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public LocalDateTime getTimeStamp() {
        return timeStamp;
    }

    public void setTimeStamp(LocalDateTime timeStamp) {
        this.timeStamp = timeStamp;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        BaseResponse<?> that = (BaseResponse<?>) o;
        return code == that.code && Objects.equals(data, that.data) && Objects.equals(message, that.message) && Objects.equals(timeStamp, that.timeStamp);
    }

    @Override
    public int hashCode() {
        return Objects.hash(code, data, message, timeStamp);
    }

    @Override
    public String toString() {
        return "BaseResponse{" +
                "code=" + code +
                ", data=" + data +
                ", message='" + message + '\'' +
                ", timeStamp=" + timeStamp +
                '}';
    }
}